package controller;

import model.MomentComment;
import model.User;
import validate.CommentValidate;

import com.jfinal.core.Controller;

import constant.Msg;

public class MomentCommentController extends Controller{
	public void index() {
		String momentId=getPara("moment_id");
		String content=getPara("content");
		String commentId=getPara("comment_id");
		String valid=null;
//		String userId="1";
		int userId=User.userCache.get(getHeader("token"));
		switch (getRequest().getMethod()) {
		case "GET":
			valid=CommentValidate.get(momentId);
			if(valid!=null){
				renderJson(valid);
				return;
			}
			renderJson(MomentComment.dao.showComments(Integer.parseInt(momentId)));
			break;
		case "POST":
			MomentComment comment=new MomentComment();
			comment.set("user_id", userId);
			comment.set("moment_id", momentId);
			comment.set("content", content);
			valid=CommentValidate.post(comment);
			if (valid!=null) {
				renderJson(valid);
				return;
			}
			renderJson(MomentComment.dao.addComment(comment));
			break;
		case "DELETE":
			valid=CommentValidate.delete(commentId,userId);
			if(valid!=null){
				renderJson(valid);
				return;
			}
			renderJson(MomentComment.dao.deleteComment(commentId));
			break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
			break;
		}
	}
}
